﻿appRoot.controller('EndpointController', function ($scope, $location, $resource, $log) {

    var endpointResource = $resource('/api/endpoints', {},
        { update: { method: 'PUT' } },
        { delete: { method: 'DELETE', params: { id: '@id' } } });

    $scope.selectedEndpoints = [];

    $scope.$watchCollection('selectedEndpoints', function () {
        $scope.selectedEndpoint = angular.copy($scope.selectedEndpoints[0]);
    });

    $scope.endpointGrid = {
        data: 'endpointList',
        multiSelect: false,
        headerClass: "{\'text-bold\'}",
        selectedItems: $scope.selectedEndpoints,
        enableColumnResize: false,
        columnDefs: [
            {
                field: 'status', displayName: '', width: '25px', cellTemplate:
                  '<div class="ngCellText"><i ng-class="{\'icon-checkmark fg-green\': row.getProperty(col.field)==0, \'icon-blocked fg-red\': row.getProperty(col.field)==1}"></i></div>'
            },
            {
                field: 'channel', displayName: '', width: '25px', cellTemplate:
                  '<div class="ngCellText" style="margin: 0 auto;"><i ng-class="{\'icon-enter fg-blue\': row.getProperty(col.field)==0, \'icon-exit fg-blue\': row.getProperty(col.field)==1}"></i></div>'
            },
            {
                field: 'name', displayName: 'Profile Name', cellTemplate:
                  '<div class="ngCellText">{{row.getProperty(col.field)}}</div>'
            },
            { field: 'address', displayName: 'IP' },
            { field: 'port', displayName: 'Port' },
            { field: 'category', displayName: 'ASTERIX Category', cellTemplate:
                '<div class="ngCellText"><div>'
                    + '<div ng-switch on="row.getProperty(\'category\')">'
                    + '<div ng-switch-when=0>CAT021 v2.1</div>'
                    + '<div ng-switch-when=1>CAT062 v0.16</div>'
                    + '<div ng-switch-default >Unknown</div>'
                    + '</div>'
                  + '</div></div>'
            }
            // { field: '', cellTemplate: '<button class="btn btn-mini btn-danger" ng-click="">Delete</button>' }
        ],
        rowTemplate: '<div ng-dblclick="open()" ng-style="{ \'cursor\': row.cursor }" ng-repeat="col in renderedColumns" ng-class="col.colIndex()" class="ngCell {{col.cellClass}}"><div class="ngVerticalBar" ng-style="{height: rowHeight}" ng-class="{ ngVerticalBarVisible: !$last }">&nbsp;</div><div ng-cell></div></div>'
    };

    $scope.getEndpoints = function () {

        $scope.endpointList = [];

        endpointResource.query(function (data) {
            $scope.endpointList = [];
            angular.forEach(data, function (endpointData) {
                $scope.endpointList.push(endpointData);
            });
        });
    };
    
    $scope.updateEndpoint = function (parameter) {

        endpointResource.update(parameter, function success(parameter) {
            $scope.selectedEndpoints[0].name = parameter.name;
            $scope.selectedEndpoints[0].address = parameter.address;
            $scope.selectedEndpoints[0].port = parameter.port;
            $scope.selectedEndpoints[0].buffer = parameter.buffer;
            $scope.selectedEndpoints[0].category = parameter.category;
            $scope.selectedEndpoints[0].channel = parameter.channel;
        });
    };

    $scope.deleteEndpoint = function (endpointId) {
        if (endpointId > -1) {
            endpointResource.delete({ id: endpointId }, function success(data) {
                var id = data[0];
                $scope.endpointList = $scope.endpointList.filter(function (el) {
                    return el.id != id;
                });
            });
        }
    };

    $scope.createEndpoint = function (parameter) {

        endpointResource.update(parameter, function success(parameter) {

            $scope.endpointList = [];

            endpointResource.query(function (data) {
                $scope.endpointList = [];
                angular.forEach(data, function (endpointData) {
                    $scope.endpointList.push(endpointData);
                });
            });

            $scope.selectedEndpoints[0] = parameter;
        });
    };

    $scope.newEndpoint = {
        address: null,
        buffer: null,
        category: null,
        channel: null,
        id: -1,
        name: null,
        port: null,
        status: null
    }
});